package com.changgou.user.service.impl;

import com.changgou.user.dao.AddressMapper;
import com.changgou.user.dao.AreasMapper;
import com.changgou.user.dao.CitiesMapper;
import com.changgou.user.dao.ProvincesMapper;
import com.changgou.user.pojo.Address;
import com.changgou.user.pojo.Areas;
import com.changgou.user.pojo.Cities;
import com.changgou.user.pojo.Provinces;
import com.changgou.user.service.AddressService;
import com.changgou.user.service.AreasService;
import com.changgou.user.service.CitiesService;
import com.changgou.user.service.ProvincesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:Address业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private CitiesService citiesService;
    @Autowired
    private ProvincesService provincesService;
    @Autowired
    private AreasService areasService;


    /**
     * Address条件+分页查询
     *
     * @param address 查询条件
     * @param page    页码
     * @param size    页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Address> findPage(Address address, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(address);
        //执行搜索
        return new PageInfo<Address>(addressMapper.selectByExample(example));
    }

    /**
     * Address分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Address> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<Address>(addressMapper.selectAll());
    }

    /**
     * Address条件查询
     *
     * @param address
     * @return
     */
    @Override
    public List<Address> findList(Address address) {
        //构建查询条件
        Example example = createExample(address);
        //根据构建的条件查询数据
        return addressMapper.selectByExample(example);
    }


    /**
     * Address构建查询对象
     *
     * @param address
     * @return
     */
    public Example createExample(Address address) {
        Example example = new Example(Address.class);
        Example.Criteria criteria = example.createCriteria();
        if (address != null) {
            // 
            if (!StringUtils.isEmpty(address.getId())) {
                criteria.andEqualTo("id", address.getId());
            }
            // 用户名
            if (!StringUtils.isEmpty(address.getUsername())) {
                criteria.andLike("username", "%" + address.getUsername() + "%");
            }
            // 省
            if (!StringUtils.isEmpty(address.getProvinceid())) {
                criteria.andEqualTo("provinceid", address.getProvinceid());
            }
            // 市
            if (!StringUtils.isEmpty(address.getCityid())) {
                criteria.andEqualTo("cityid", address.getCityid());
            }
            // 县/区
            if (!StringUtils.isEmpty(address.getAreaid())) {
                criteria.andEqualTo("areaid", address.getAreaid());
            }
            // 电话
            if (!StringUtils.isEmpty(address.getPhone())) {
                criteria.andEqualTo("phone", address.getPhone());
            }
            // 详细地址
            if (!StringUtils.isEmpty(address.getAddress())) {
                criteria.andEqualTo("address", address.getAddress());
            }
            // 联系人
            if (!StringUtils.isEmpty(address.getContact())) {
                criteria.andEqualTo("contact", address.getContact());
            }
            // 是否是默认 1默认 0否
            if (!StringUtils.isEmpty(address.getIsDefault())) {
                criteria.andEqualTo("isDefault", address.getIsDefault());
            }
            // 别名
            if (!StringUtils.isEmpty(address.getAlias())) {
                criteria.andEqualTo("alias", address.getAlias());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        Address address = new Address();
        address.setId(id);
//        addressMapper.deleteByPrimaryKey(address);
        addressMapper.deleteByExample(createExample(address));
    }

    /**
     * 修改Address
     *
     * @param address
     */
    @Override
    public void update(Address address) {
//        addressMapper.updateByPrimaryKey(address);
        Address addr = new Address();
        addr.setId(address.getId());
        addressMapper.updateByExampleSelective(address, createExample(addr));
    }

    /**
     * 增加Address
     *
     * @param address
     */
    @Override
    public void add(Address address) {
        //用户的地址最多不能超过20条
        Address address1 = new Address();
        address1.setUsername(address.getUsername());
        int count = addressMapper.selectCountByExample(createExample(address1));
        if (count >= 20) {
            throw new RuntimeException("添加失败，一个用户最多可以添加20条收货地址");
        }
        addressMapper.insertSelective(address);
    }

    /***
     * 根据ID查询Address
     * @param id
     * @return
     */
    @Override
    public Address findById(Integer id) {
        Address address = new Address();
        address.setId(id);
        address = addressMapper.selectByExample(createExample(address)).get(0);
        return address;
    }

    /***
     * 查询Address全部数据
     * @return
     */
    @Override
    public List<Address> findAll() {
        return addressMapper.selectAll();
    }

    @Override
    public PageResult<Address> findByUsername(String username) {
        Address address = new Address();
        address.setUsername(username);
        List<Address> list = addressMapper.selectByExample(createExample(address));
        list = buildAddressList(list);
        Long size = Long.valueOf(list.size());
        return new PageResult<Address>(size, list);
    }

    /***
     * 设置用户默认收货地址
     * @param username
     * @param id
     */
    @Override
    public void setDefault(String username, Integer id) {
        Address address = new Address();
        address.setUsername(username);
        Example example = createExample(address);
        address.setIsDefault("0");
        //1.清除用户原来的默认设置
        addressMapper.updateByExampleSelective(address, example);
        //2.设置默认地址
        address.setId(id);
        example = createExample(address);
        address.setIsDefault("1");
        addressMapper.updateByExampleSelective(address, example);
    }

    /***
     * 封装省、市、区对象
     * @param list
     * @return
     */
    public List<Address> buildAddressList(List<Address> list) {
        if (list != null) {
            for (Address address : list) {
                Provinces provinces = provincesService.findById(address.getProvinceid());
                Cities cities = citiesService.findById(address.getCityid());
                Areas areas = areasService.findById(address.getAreaid());
                address.setProvinces(provinces);
                address.setCities(cities);
                address.setAreas(areas);
            }
            return list;
        }
        return null;
    }
}
